package com.astar.controller.sort;


import com.astar.entity.CustomChapter;
import com.astar.entity.PractiseCourseChapter;
import com.astar.entity.PractiseCourseType;
import com.astar.service.IPractiseCourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类练习-科目-章节表  前端控制器
 * 章节管理
 * </p>
 *
 * @author jobob
 * @since 2020-01-31
 */

@Controller
@RequestMapping("/chapterManage")
public class PractiseCourseChapterController {

    @Autowired
    private IPractiseCourseChapterService practiseCourseChapterServiceImpl;

    @RequestMapping("/index")
    public Object toIndex(Model model){
        List<PractiseCourseType> course = practiseCourseChapterServiceImpl.selectCourse();
        model.addAttribute("data",course);
        return "chapterManage/index";
    }

    @RequestMapping("/selectList")
    @ResponseBody
    public Object selectList(CustomChapter customChapter){
        List<CustomChapter> practiseCourseTypes = practiseCourseChapterServiceImpl.selectAll(customChapter);
        return practiseCourseTypes;
    }

    @RequestMapping("/toAdd")
    public Object toAdd(Model model){
        List<PractiseCourseType> course = practiseCourseChapterServiceImpl.selectCourse();
        model.addAttribute("data",course);
        return "chapterManage/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(PractiseCourseChapter practiseCourseChapter){
        int i = practiseCourseChapterServiceImpl.add(practiseCourseChapter);
        return i;
    }

    @RequestMapping("/deletes")
    @ResponseBody
    public Object deleteForIds(String arr){
        Map<String,Object> map = new HashMap<>();
        String[] array = arr.split(",");
        for (int i =0;i<array.length;i++){
            int id = Integer.parseInt(array[i]);
            PractiseCourseChapter practiseCourseChapter = new PractiseCourseChapter();
            practiseCourseChapter.setId(id);
            practiseCourseChapterServiceImpl.deleteForIds(practiseCourseChapter);
        }
        map.put("returnCode","0000");
        return map;
    }

    @RequestMapping("/toUpdate")
    public Object toUpdate(Integer id,Model model){
        List<PractiseCourseType> course = practiseCourseChapterServiceImpl.selectCourse();
        model.addAttribute("data1",course);
        PractiseCourseChapter practiseCourseChapter = practiseCourseChapterServiceImpl.selectForId(id);
        model.addAttribute("data",practiseCourseChapter);
        return "chapterManage/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(PractiseCourseChapter practiseCourseChapter){
        Map<String,Object> map = new HashMap<>();
        int i = practiseCourseChapterServiceImpl.updateForId(practiseCourseChapter);
        map.put("returnCode","0000");
        return map;
    }


    @RequestMapping("/view")
    public Object view(Integer id,Model model) {
        List<PractiseCourseType> course = practiseCourseChapterServiceImpl.selectCourse();
        model.addAttribute("data1",course);
        PractiseCourseChapter practiseCourseChapter = practiseCourseChapterServiceImpl.selectForId(id);
        model.addAttribute("data",practiseCourseChapter);
        return "chapterManage/view";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteForId(PractiseCourseChapter practiseCourseChapter){
        Map<String,Object> map = new HashMap<>();
        int i = practiseCourseChapterServiceImpl.deleteForIds(practiseCourseChapter);
        if (i!=0){
            map.put("returnCode","0000");
        }
        return map;
    }







}
